package com.shanghaionstar.discover.dealer.presenter;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.mag.dealer.PreferredDealerEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.DiscoverActivity;
import com.shanghaionstar.discover.dealer.ChangePreferDealerFragment;
import com.shanghaionstar.discover.dealer.iview.IPreferDealerView;
import com.shanghaionstar.discover.dealer.service.PreferDealerService;
import com.shanghaionstar.utils.AlertDialogUtil;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/7/19.
 */
public class PreferDealerPresenter extends BasePresenter<Object> {
    private IPreferDealerView mPreferDealerView;
    private BaseActivity mContext;
    private PreferDealerService mService;
    private PreferredDealerEntity preEntity;

    public PreferDealerPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        mPreferDealerView = (IPreferDealerView) view;
        mContext = (BaseActivity) mPreferDealerView.onActivity();
        mService = new PreferDealerService(mContext);
    }

    public void getPreferDealer() {
        mPreferDealerView.setLoading(true);
        subscription = mService.getPerferDealer().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<PreferredDealerEntity>() {
                    @Override
                    public void call(PreferredDealerEntity dealerResponse) {
                        mPreferDealerView.setLoading(false);
                        preEntity = dealerResponse;
                        mPreferDealerView.onGetPreferDealer(dealerResponse);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mPreferDealerView.setLoading(false);
                        mPreferDealerView.onGetPreferDealerFailed();
                    }
                });
    }

    public void changePerferDealer() {
        mPreferDealerView.setLoading(true);
        subscription = mService.changePerferDealer(preEntity == null ? "" : preEntity.getId()).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String json) {
                        mPreferDealerView.setLoading(false);
                        mPreferDealerView.onReponseSuccess(json);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mPreferDealerView.setLoading(false);
                        if (throwable != null)
                            mPreferDealerView.onResponseError((OnStarException) throwable);
                    }
                });
    }

    public void switchToChangePreferDealer() {
//        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.change_prefer_dealer.getStringValue());
        ChangePreferDealerFragment fragment = new ChangePreferDealerFragment();
        Bundle bundle = new Bundle();
        if (preEntity != null) {
            bundle.putString("partyID", preEntity.getId());
            fragment.setArguments(bundle);
        }
        mContext.changeFragment(mContext.getSupportFragmentManager(), fragment, ChangePreferDealerFragment.class, true);
    }

    public void showCall() {
        if (!CommonUtil.isNull(preEntity) && !CommonUtil.isNull(preEntity.getTelephone())) {
            AlertDialogUtil.showConfigureDialog(mContext,
                    mContext.getString(R.string.dialog_warning),
                    preEntity.getTelephone(),
                    mContext.getString(R.string.call_out),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mContext.requestRunTimePermissions(new String[]{Manifest.permission.CALL_PHONE}, new PermissionCall() {
                                @Override
                                public void requestSuccess() {
                                    Intent callIntent = new Intent(Intent.ACTION_CALL,
                                            Uri.parse("tel:" + preEntity.getTelephone()));
                                    mContext.startActivity(callIntent);
                                }

                                @Override
                                public void refused() {

                                }
                            });

                        }
                    });
        }
    }

    public void getCacheDealer() {
        if (mContext instanceof DiscoverActivity) {
            preEntity = ((DiscoverActivity) mContext).getPreEntity();
        }
        if (preEntity != null)
            mPreferDealerView.onGetCachePreferDealer(preEntity);
    }
}
